<template>
    <div ref="chart" style="width: 100%; height: 400px;"></div>
</template>

<script>
import * as echarts from 'echarts';

export default {
    props: {
        data: Object
    },
    mounted() {
        this.chart = echarts.init(this.$refs.chart);
        this.updateChart();
    },
    watch: {
        data: {
            deep: true,
            handler() {
                this.updateChart();
            }
        }
    },
    methods: {
        updateChart() {
            this.chart.setOption({
                tooltip: {
                    trigger: 'axis',
                    axisPointer: {
                        type: 'shadow'
                    }
                },
                xAxis: {
                    type: 'category',
                    data: this.data.labels
                },
                yAxis: {
                    type: 'value'
                },
                series: [
                    {
                        type: 'bar',
                        data: this.data.datasets[0].data,
                        itemStyle: {
                            color: this.data.datasets[0].backgroundColor
                        }
                    }
                ]
            });
        }
    }
}
</script> 